package com.lzw.breezeNovel.webApp.service.impl.video;

import com.lzw.breezeNovel.model.entity.pojo.video.Anime;
import com.lzw.breezeNovel.model.entity.pojo.video.Movie;
import com.lzw.breezeNovel.model.entity.pojo.video.TvSeries;
import com.lzw.breezeNovel.model.entity.query.video.CategoryQuery;
import com.lzw.breezeNovel.model.enums.viedo.AnimeStatus;
import com.lzw.breezeNovel.model.enums.viedo.TvStatus;
import com.lzw.breezeNovel.webApp.mapper.video.AnimeMapper;
import com.lzw.breezeNovel.webApp.mapper.video.MovieMapper;
import com.lzw.breezeNovel.webApp.mapper.video.TvSeriesMapper;
import com.lzw.breezeNovel.webApp.mapper.video.VideoCategoryMapper;
import com.lzw.breezeNovel.webApp.service.video.VideoCategoryService;
import com.lzw.breezeNovel.webApp.vo.video.AnimeVo;
import com.lzw.breezeNovel.webApp.vo.video.MovieVo;
import com.lzw.breezeNovel.webApp.vo.video.TvSeriesVo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.video
 * @date 2025/3/18
 * @description 视频分类
*/

@Slf4j
@Service
@SuppressWarnings("all")
public class VideoCategoryServiceImpl implements VideoCategoryService {

    /**
     * 注入mapper
     */
    @Autowired
    private VideoCategoryMapper videoCategoryMapper;

    /**
     * 注入动漫mapper
     */
    @Autowired
    private AnimeMapper animeMapper;

    /**
     * 注入电影mapper
     */
    @Autowired
    private MovieMapper movieMapper;

    /**
     * 注入电视剧mapper
     */
    @Autowired
    private TvSeriesMapper tvMapper;


    /**
     * 分类获取动漫
     *
     * @param categoryQuery
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<AnimeVo> getCategoryData(@NotNull CategoryQuery categoryQuery) {
        //计算分页
        int pageNo = startPage(categoryQuery.getPage(), categoryQuery.getSize());
        //设置分页参数
        categoryQuery.setPage(pageNo);
        //查询数据
        List<Anime> categoryData = animeMapper.SelectCategoryData(categoryQuery);
        //进行数据拷贝
        List<AnimeVo> comicVoList = CopyAnime(categoryData);
        return comicVoList;
    }

    /**
     * 分类获取电影
     *
     * @param categoryQuery
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<MovieVo> getMovieData(@NotNull CategoryQuery categoryQuery) {
        //计算分页
        int pageNo = startPage(categoryQuery.getPage(), categoryQuery.getSize());
        //设置分页参数
        categoryQuery.setPage(pageNo);
        //获取分页数据
        List<Movie> movieVoList = movieMapper.getMovieData(categoryQuery);
        //进行数据拷贝
        List<MovieVo> movieVoList1 = CopyMovie(movieVoList);
        return movieVoList1;
    }

    /**
     * 分类获取电视剧
     *
     * @param categoryQuery
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<TvSeriesVo> getTvSeriesData(@NotNull CategoryQuery categoryQuery) {
        //计算分页
        int pageNo = startPage(categoryQuery.getPage(), categoryQuery.getSize());
        //设置分页参数
        categoryQuery.setPage(pageNo);
        //查询数据
        List<TvSeries> tvSeriesData = tvMapper.getTvSeriesData(categoryQuery);
        //拷贝数据
        List<TvSeriesVo> tvSeriesVoList = CopyTvSeries(tvSeriesData);
        return tvSeriesVoList;
    }


    /**
     * 拷贝电视剧
     * @param tvSeries
     * @return
     */
    @NotNull
    private List<TvSeriesVo> CopyTvSeries(List<TvSeries> tvSeries) {
        //判断数据是否为空
        if (tvSeries == null || tvSeries.isEmpty()) {
            return Collections.emptyList();
        }
        //拷贝数据
        return tvSeries.stream().map(TvSerie ->{
            TvSeriesVo tvSeriesVo = new TvSeriesVo();
            //进行对象拷贝
            BeanUtils.copyProperties(TvSerie,tvSeriesVo);
            //获取状态
            Integer status = TvSerie.getStatus();
            //判断状态
            if(status == TvStatus.SERIALIZE.getCode())
            {
                tvSeriesVo.setStatus(TvStatus.SERIALIZE.getDesc());
            }else
            {
                tvSeriesVo.setStatus(TvStatus.FINISH.getDesc());
            }
            return tvSeriesVo;
        }).toList();

    }

    /**
     * 拷贝电影
     * @param movieVos
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<MovieVo> CopyMovie(List<Movie> movieVos) {
        //判断数是否存在
        if (movieVos == null || movieVos.size() == 0) {
            return null;
        }
        return movieVos.stream().map(movie -> {
            MovieVo movieVo = new MovieVo();
            BeanUtils.copyProperties(movie, movieVo);
            return movieVo;
        }).collect(Collectors.toList());
    }


    /**
     * 拷贝数据动漫
     * @param animeList
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<AnimeVo> CopyAnime(List<Anime> animeList) {
        //判断是否为空
        if (animeList == null || animeList.size() == 0) {
            return null;
        }
        return animeList.stream()
                .map(anime -> {
                    AnimeVo animeVo = new AnimeVo();
                    BeanUtils.copyProperties(anime, animeVo);
                    //获取动漫状态
                    Integer status = anime.getStatus();
                    if(AnimeStatus.SERIAL.getCode() == status){
                        animeVo.setStatus(AnimeStatus.SERIAL.getDesc());
                    }else
                    {
                        animeVo.setStatus(AnimeStatus.FINISH.getDesc());
                    }
                    return animeVo;
                })
                .collect(Collectors.toList());
    }

    /**
     * 计算分页
     * @param pageNo
     * @param size
     * @return
     */
    public int startPage(Integer pageNo, Integer size){
        //判断分页数据是否为空
        pageNo = Optional.of(pageNo).orElse(1);
        size = Optional.of(size).orElse(10);
        pageNo = Math.max(pageNo, 1);
        size = Math.max(size, 1);
        //计算分页
        pageNo = (pageNo - 1) * size;
        return pageNo;
    }


}
